package cn.azzhu.day02.transformations;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @author azzhu
 * @create 2020-09-18 11:14:35
 */
public class MaxDemo {
    public static void main(String[] args) throws Exception {
        final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        //spark,10
        final DataStreamSource<String> lines = env.socketTextStream("hadoop105", 9999);

        final SingleOutputStreamOperator<Tuple2<String, Integer>> wordAndNum = lines.map(new MapFunction<String, Tuple2<String, Integer>>() {
            @Override
            public Tuple2<String, Integer> map(String line) throws Exception {
                final String[] fields = line.split(",");
                return Tuple2.of(fields[0], Integer.parseInt(fields[1]));
            }
        });

        final KeyedStream<Tuple2<String, Integer>, Tuple> keyed = wordAndNum.keyBy(0);

        final SingleOutputStreamOperator<Tuple2<String, Integer>> res = keyed.max(1);

        res.print();

        env.execute("MaxDemo");
    }
}
